Relational Operators

The IDL relational operators apply a relation to two operands and return a value of true (1) or false (0). The resulting value can be used as the predicate in IF, WHILE or REPEAT statements. You can also combine operators with other logical values to make more complex expressions.

Note: It is important to see Definition of True and False for details on when a value is considered true or false.

Note: If one or both of the operands are objects, the operator may be overloaded.

The rules for evaluating relational expressions with operands of mixed modes are the same as for arithmetic expressions. Each operand is promoted to the data type of the operand with the greatest precedence or potential precision. (See Data Type and Structure of Expressions for details.) For example, in the relational expression “2 EQ 2.0”, the integer 2 is converted to floating point and compared to the floating point 2.0. The result of this expression is true (1). The type of the result is always byte.

Note: Signed and unsigned integers of a given width have the same precedence. In an expression involving a combination of such types, the result is given the type of the leftmost operand.

Note: When using EQ and NE with complex numbers, both the real and imaginary parts must meet the condition of the relational operator. For example, the following returns 0 (false):
   PRINT, COMPLEX(1,2) EQ COMPLEX(1,-2)

When using GE, GT, LE, and LT with complex numbers, the absolute value (or modulus) of the complex number is used for the comparison.

For more information on using relational operators, also see Using Relational Operators with Arrays and Relational Operators with Infinity and NaN Values.

Operator

Description

Example

EQ

Equal to

Returns true if its operands are equal; otherwise, it returns false. The following returns True:

IF (2 EQ 2.0) THEN PRINT, 'True'

For arrays, lists, and hashes, the EQ operator does an element-by-element comparison, and returns a byte array of 1s and 0s.

NE

Not equal to

 

Returns true whenever the operands are different. The following returns 1 (true):

PRINT, "sun" NE "fun"

For arrays, lists, and hashes, the NE operator behaves in the opposite manner of EQ.

GE

Greater than or equal to

 

Returns true if the operand on the left is greater than or equal to the one on the right. Relational operator are useful for creating array masks:

A = ARRAY * (ARRAY GE 100)

See Using Relational Operators with Arrays.

GT

Greater than

 

 

 

Returns true if the operand on the left is greater than the operand on the right. Determine if A is greater than B:

IF (A GT B) THEN PRINT, 'True'

Note: Strings are compared using the ASCII collating sequence: “ “ is less than “0” is less than “9” is less than “A” is less than “Z” is less than “a” which is less than “z”.

LE

Less than or equal to

 

Returns true if the operand on the left is less than or equal to the operand on the right. Determine if A is less than or equal to B:

IF (A LE B) THEN PRINT, 'True'

LT

Less than

Returns true if the operand on the left is less than the operand on the right. Determine if A is less than B:

IF (A LT B) THEN PRINT, 'True'

Note: You can use the NE and EQ operators to determine if two object references point to the same heap variable. See Object Equality and Inequality for examples.

Using Relational Operators with Arrays

Relational operators can be applied to arrays, and the resulting array of ones and zeroes can be used as an operand. For example, the expression:

A = ARR * (ARR LE 100)

A is an array equal to ARR except that all points greater than 100 have been reduced to zero. The expression (ARR LE 100) is an array that contains a 1 where the corresponding element of ARR is less than or equal to 100, and zero otherwise. For example, to print the number of positive elements in the array ARR:

PRINT, TOTAL(ARR GT 0)

The following command sets B equal to ARRAY whenever the corresponding element of ARRAY is greater than or equal to 100. If the element is less than 100, the corresponding element of B is set to zero.

B = ARRAY * (ARRAY GE 100)

Relational Operators with Infinity and NaN Values

On the Windows platform, using relational operators with the values infinity or NaN (Not a Number) causes an “illegal operand” error. The FINITE function’s INFINITY and NAN keywords can be used to perform comparisons involving infinity and NaN values. For more information, see FINITE.